<template>
  <div class="order-complaint-bg">
    <el-card class="order-complaint-card">
      <div class="order-complaint-header">
        <el-button @click="goBack" icon="ArrowLeft">返回</el-button>
        <div class="order-complaint-title">订单投诉</div>
      </div>
      <el-descriptions :column="1" border v-if="order">
        <el-descriptions-item label="订单编号">{{ order.order_number }}</el-descriptions-item>
        <el-descriptions-item label="起点">{{ order.start_address }}</el-descriptions-item>
        <el-descriptions-item label="终点">{{ order.end_address }}</el-descriptions-item>
      </el-descriptions>
      <el-form :model="form" label-width="80px" class="complaint-form">
        <el-form-item label="投诉类型">
          <el-select v-model="form.complaint_type" placeholder="请选择投诉类型">
            <el-option label="服务态度差" value="service_attitude" />
            <el-option label="车辆不整洁" value="vehicle_cleanliness" />
            <el-option label="路线不合理" value="route_issue" />
            <el-option label="收费问题" value="payment_issue" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="投诉内容">
          <el-input v-model="form.complaint_content" type="textarea" rows="4" placeholder="请详细描述投诉内容" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitComplaint" :loading="loading">提交投诉</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { http } from '../utils/request'
import { ElMessage } from 'element-plus'
import { orderAPI, orderComplaintAPI } from '@/api/order'
import axios from 'axios'
const route = useRoute()
const router = useRouter()
const orderId = route.params.id
const order = ref(null)
const form = ref({ complaint_type: '', complaint_content: '', defendant_id: null})
const loading = ref(false)

async function fetchOrder() {
    // 通过订单编号查询
    const res = await axios.get(`http://127.0.0.1:8000/order/orders/${orderId}/`)
    if (res.data) {
      order.value = res.data
    }
}

function goBack() {
  router.go(-1)
}

async function submitComplaint() {
  if (!form.value.complaint_type) {
    ElMessage.warning('请选择投诉类型')
    return
  }
  if (!form.value.complaint_content) {
    ElMessage.warning('请填写投诉内容')
    return
  }
  loading.value = true
  try {
    await orderComplaintAPI.createComplaint({
      order: orderId,
      complaint_type: form.value.complaint_type,
      content: form.value.complaint_content,
      complainant_id: order.value.passenger_id,
      defendant_id:order.value.driver_id
    })
    ElMessage.success('投诉提交成功')
    router.replace(`/order/detail/${orderId}`)
  } catch (e) {
    ElMessage.error('投诉提交失败')
  } finally {
    loading.value = false
  }
}

onMounted(()=>{
  fetchOrder()
})
</script>

<style scoped>
.order-complaint-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #fff 60%, #e6f7ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.order-complaint-card {
  width: 480px;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(34,34,34,0.08);
  padding: 32px 36px 24px 36px;
  position: relative;
}
.order-complaint-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}
.order-complaint-title {
  font-size: 26px;
  font-weight: bold;
  color: #222;
  text-align: center;
  margin: 0 auto;
  letter-spacing: 2px;
}
.complaint-form {
  margin-top: 24px;
}
</style> 